<!DOCTYPE html>
<html lang="en">

<!-- 
     2018-12-07 发现及时是Js 在W3c 上的官方代码也运行出错 

 -->
<!-- <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>JS Cookies</title>
    <script type="text/javascript">
        // JS Cookies 和 Java中的 JSP 中的cookies  差不多
        /*
        名字 cookie
        密码 cookie
        日期 cookie
        是常用的三种 创建和存储cookie 
        */
        function checkCookie(){
            username = getCookie('username')
            if (username != null && username != ""){
                alert("Welcome again" + username + "!")
            }else{
                username = prompt("Please inter your name:","")
                if(username != null && username != ""){
                    setCookie('username',username,365)
                    alert(username)
                }
            }
        }
        
        // 设置 cookie 的过期时间和取的name
        function setCookie(c_name,value,expiredays){
            var exdate = new Date()
            exdate.setDate(exdate.getDate() + expiredays)
            var date = (expiredays==null) ? "" : ";expires="+ exdate.toGMTString()
            document.cookie = c_name + escape(value) + date;
        }
        
        // 这种取 cookie 的方式有点复杂 
        function getCookie(c_name){
            var cookie = document.cookie
            if (document.cookie.length > 0){
                var c_start = document.cookie.indexOf(c_name + "=")
                if(c_start != -1){
                    c_start = c_start + c_name.length + 1
                    var c_end = document.cookie.indexOf(";",c_start)
                    if(c_end == -1){
                        c_end = document.cookie.length
                    }
                    return unescape(document.cookie.substring(c_start,c_end))
                }
            }
            return ""
        }

    </script>
</head>
<body onload="checkCookie()">
    <div>如果设置了cookie 第二次会弹出之前设置的值</div>
</body> -->


<head>
    <script type="text/javascript">
    function getCookie(c_name)
    {
    if (document.cookie.length>0)
      {
      c_start=document.cookie.indexOf(c_name + "=")
      if (c_start!=-1)
        { 
        c_start=c_start + c_name.length+1 
        c_end=document.cookie.indexOf(";",c_start)
        if (c_end==-1) c_end=document.cookie.length
        return unescape(document.cookie.substring(c_start,c_end))
        } 
      }
    return ""
    }
    
    function setCookie(c_name,value,expiredays)
    {
    var exdate=new Date()
    exdate.setDate(exdate.getDate()+expiredays)
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
    }
    
    function checkCookie()
    {
    username=getCookie('username')
    if (username!=null && username!="")
      {alert('Welcome again '+username+'!')}
    else 
      {
      username=prompt('Please enter your name:',"")
      if (username!=null && username!="")
        {
        setCookie('username',username,365)
        }
      }
    }
    </script>
    </head>

    <body onLoad="checkCookie()">
    </body>
</html>